﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using WindowsGameLibrary1;
using Nuclex.Game.States;
using Nuclex.Input;
using Microsoft.Xna.Framework.Audio;

namespace DataModels
{
    public class BaseGame : Microsoft.Xna.Framework.Game
    {

        protected InputHandler input;
        protected FPS fps;
        protected StateManager gameStateManager;
        protected GraphicsDeviceManager graphics;
        protected SpriteBatch spriteBatch;
        protected Rectangle titleSafeArea;

        //sound
        protected SoundManager soundManager;

        public BaseGame()
        {
            GameExiter.setExiter(this);
            Content.RootDirectory = "Content";
            gameStateManager = new StateManager();
            input = new InputHandler(this);

            ComputerInput.createComputerInput(Services, Window.Handle, Components);

            Components.Add(input);
            graphics = new GraphicsDeviceManager(this);
            setWindowDimensions(graphics, 1000, 650);
            setFPSInMillisecounds(50000);

        }

        private void setWindowDimensions(GraphicsDeviceManager graphics, int width, int height)
        {
            graphics.PreferredBackBufferWidth = width;
            graphics.PreferredBackBufferHeight = height;
        }

        private void setFPSInMillisecounds(int millisecounds)
        {
#if DEBUG
            TimeSpan frames = new TimeSpan(millisecounds);

            //draw 60 fps and update as often as possible
            fps = new FPS(this, true, true, frames);
#else
            fps = new FPS(this, true, false,new System.TimeSpan(0,0,millisecounds/1000));
#endif
            Components.Add(fps);
        }

        protected override void Initialize()
        {
            // TODO: Add your initialization logic here

            titleSafeArea = Utility.GetTitleSafeArea(graphics.GraphicsDevice, 0.85f);

            base.Initialize();
        }

        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(graphics.GraphicsDevice);


            base.LoadContent();
        }

        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
            base.UnloadContent();
        }

        protected override void Update(GameTime gameTime)
        {
            gameStateManager.Update(gameTime);
            base.Update(gameTime);
        }



        protected override void Draw(GameTime gameTime)
        {
            graphics.GraphicsDevice.Clear(Color.CornflowerBlue);

            spriteBatch.Begin();
            gameStateManager.Draw(gameTime);
            spriteBatch.End();

            base.Draw(gameTime);
        }

        //public Microsoft.Xna.Framework.Content.ContentManager returnContentDir()
        //{
        //    return Content;
        //}

        ////public InputHandler returnGameInput()
        ////{
        ////    return input;
        ////}

        //public SpriteBatch getSpriteBatch()
        //{
        //    return spriteBatch;
        //}


        //public GraphicsDevice getGraphicsDevice()
        //{
        //    return GraphicsDevice;
        //}

    }
}
